package com.miracle9.game.dao;

import com.miracle9.game.entity.PayApply;
import com.miracle9.game.util.MyUtil;
import org.hibernate.Query;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class PayApplyServiceDao extends SqlOperating<PayApply, Integer> {

    @Cacheable(value = {"springCache"}, key = "'getAllPayApply'")
    public List<PayApply> getAllPayApply() {
        return q_CreateSqlList("from PayApply where state=0", new Object[0]);
    }

    public boolean updateStatus(int id, PayApply.PayApplyState state) {
        Query query = q_CreateSql("update PayApply set state = ?,datetime=? where state=0 and id=?", new Object[]{state,
                MyUtil.dateFormatYMD_HMS_Sting(new Date()), Integer.valueOf(id)});
        int count = query.executeUpdate();
        return count > 0;
    }

    public PayApply getByuserIdAndUserType_0(int userId, int payType) {
        return (PayApply) q_CreateSqlFilter("from PayApply where userId=? and state=0 and userType=0 and type=?", new Object[]{Integer.valueOf(userId), Integer.valueOf(payType)});
    }

    public PayApply getByuserIdAndUserType_1(int promoterId, int payType) {
        return (PayApply) q_CreateSqlFilter("from PayApply where userId=? and state=0 and userType=1 and type=?", new Object[]{Integer.valueOf(promoterId), Integer.valueOf(payType)});
    }

    public boolean getQuantity() {
        int count = q_CreateSqlInt("select count(*) from PayApply where state=0 and userType=0", new Object[0]);
        return count > 0;
    }

    public void updateNotice(int id, boolean notice) {
        q_CreateSql("update PayApply set notice = ? where id=?", new Object[]{Boolean.valueOf(notice), Integer.valueOf(id)}).executeUpdate();
    }

    public boolean updateState_2ByUserIdAndUserType_0(int userId) {
        int cnt = q_CreateSql("update PayApply set state=2 where type=1 and state=0 and userType=0 and userId=", new Object[]{Integer.valueOf(userId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateState_2ByUserIdAndUserType_1(int childPromoterId) {
        int cnt = q_CreateSql("update PayApply set state=2 where type=1 and state=0 and userType=1 and userId=?", new Object[]{Integer.valueOf(childPromoterId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateState_1ByUserIdAndUserType_0(int userId) {
        int cnt = q_CreateSql("update PayApply set state=1 where type=1 and state=0 and userType=0 and userId=", new Object[]{Integer.valueOf(userId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public boolean updateState_1ByUserIdAndUserType_1(int userId) {
        int cnt = q_CreateSql("update PayApply set state=1 where type=1 and state=0 and userType=1 and userId=?", new Object[]{Integer.valueOf(userId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

    public PayApply getPayApplyByUserId(int userId) {
        return (PayApply) q_CreateSqlFilter("from PayApply where userId=? and state=0 and userType=1 and type=0", new Object[]{Integer.valueOf(userId)});
    }

    public boolean updateStateAndNotice(int applyId, PayApply.PayApplyState state, boolean notice) {
        int cnt = q_CreateSql("update PayApply set state=?,notice=? where id=?", new Object[]{state, Boolean.valueOf(notice), Integer.valueOf(applyId)}).executeUpdate();
        if (cnt > 0) {
            return true;
        }
        return false;
    }

}
